import Platform = require('./Platform'); 
// import httpUtils = require("./XmlHttpTool"); //可以是js也可以是ts
export default class DefaultPlatform extends Platform {
    init(bg) { 
        this.bg=bg;
        this.params = {
            devid: 6, devtype: 2, appid: "com.taoqi001.mp_marmot",
            appver: 300, token: "8bba0bcbdd4b6a768c90aa3cf4ea7283"        
        };

    }

    getWeXinToken(callback: Function){
       
        wx.login({
            success: (res) => {
            
                callback(res["code"]);
                
            },
            fail: (res) => {
                console.log('微信登录失败')
            },
            complete: (res) => { }
        })

    }

    getWeXinUserInfo(callUser: Function){

       
        let btnX = (cc.winSize.width - this.bg.startGameBtn.node.width) / 2;
        let btnY = (cc.winSize.height - this.bg.startGameBtn.node.height) / 2 - this.bg.startGameBtn.node.y;
        let system = wx.getSystemInfoSync();
        let width = system.screenWidth;
        let height = system.screenHeight;
        let thisWidth = cc.winSize.width;
        let thisHeight = cc.winSize.height;

         console.log("x.y",btnX,btnY)
        var button = wx.createUserInfoButton({
            type: 'text',
            text: '',
            style: {
            left: btnX / thisWidth * width,
            top: btnY / thisHeight * height,
            width: this.bg.startGameBtn.node.width / thisWidth * width,
            height: this.bg.startGameBtn.node.height / thisHeight * height,
            lineHeight: 40,
            backgroundColor: '#00000000',
            color: '#ffffff',
            textAlign: 'center',
            fontSize: 16,
            borderRadius: 4
            }
            })

        this.button=button
        this.button.show()

        this.button.onTap((res) =>{

            this.bg.enterGameViewClick();
             button.hide()
             console.log("全局2",window["httppost"])
             window["httppost"].NhttpPost("/users/save_user_info",{
                avatar: res.userInfo.avatarUrl,
                name: res.userInfo.nickName,
                gender: res.userInfo.gender
            },function(userres){
                
                callUser(userres)
                console.log("用户信息",userres)
            })
        })
    
    }

    wxShareMessage(){

        let height = canvas.height;
        let width = canvas.width;
        let originH = (cc.winSize.height - 634) / 2 - 109;
        let y = height * originH / cc.winSize.height;

        wx.shareAppMessage({
            title: '淘气打地鼠大作战',
            imageUrl:canvas.toTempFilePathSync({
                width:width ,
                height: height - y,
                x: 0,
                y: y
              })
            ,
            success:function(res){

            console.log("转发成功")
            },
            fail:function(failres){
                console.log("转发失败")
            }
          })

        var that=this
          window["httppost"].NhttpPost("/marmot/share",{},function(sharees){
        
            console.log("分享调用",sharees)
             
            that.bg.getUserInfoMessage()


        })


    }

   

    setStorage(obj: Object) {

        wx.setStorageSync(obj['key'], obj['value']);
        this.params["token"]=obj['value']
        console.log("改变的字典",this.params)
   
    }

    getStorage(key) {

       
        return wx.getStorageSync(key);

    }


    checkSession() {

        wx.checkSession({
            success: (res) => {//未过期
                //直接登录
                
                let data = wx.getStorageSync("wxtoken");
                this.params["token"]=data
                this.bg.loginMouse();
                console.log("未过期",res)

            },
            fail: (res) => { //已过期
             
                console.log("已过期",res)
                //重新登录
                this.bg.loginGame();

            },
            complete: (res) => { }
        });
    }


}
